package com.yc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.R_Code;
import com.yc.Result;
import com.yc.entities.BloodSugarRole;
import com.yc.entities.custom.*;
import com.yc.mapper.BloodSugarRoleMapper;
import com.yc.service.BloodSugarRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author kuakha
 * @since 2021-04-07
 */
@Service
public class BloodSugarRoleServiceImpl extends ServiceImpl<BloodSugarRoleMapper, BloodSugarRole> implements BloodSugarRoleService {

    @Override
    public Result getAllRoleInfo(CustomRoleQueryRequestBean customRoleQueryRequestBean) {
        Page<CustomRoleQueryRequestBean> page = new Page<>(customRoleQueryRequestBean.getCurrent(), customRoleQueryRequestBean.getSize());
        List<CustomRoleQueryResponseBean> allRoleInfo = this.baseMapper.getAllRoleInfo(page, customRoleQueryRequestBean);
        if (page.getTotal() >= 1) {
            return Result.ok().total((int) page.getTotal()).data(allRoleInfo);
        } else {
            return Result.error().code(R_Code.R_NULL_DATA_FAIL);
        }
    }

    @Override
    public Result getUserInfoByUserId(Integer userId, Integer deanId) {
        if (userId != null && deanId != null) {
            CustomRoleManageViewAndUpdateBean roleInfo = baseMapper.getRoleInfoById(userId, deanId);
            Result result = Result.ok();
            result.data("roleInfo", roleInfo);
            result = getPermissionsByUserAndDean(result, userId, deanId);
            result = getAllPermissionToAdd(result, deanId, deanId);
            return result;

        } else {
            return new Result().error();
        }
    }

    @Override
    public Result getPermissionsByUserAndDean(Result result
            , Integer userId
            , Integer deanId) {
        if (userId != null && deanId != null) {
            List<CustomRoleManageViewAndUpdatePullDownResponseBean> pcList = baseMapper.getInfoFromUser(userId, deanId, "0",null,null,null,null);
            List<CustomRoleManageViewAndUpdatePullDownResponseBean> appList = baseMapper.getInfoFromUser(userId, deanId, "1",null,null,null,null);
            result.data("roleInfoPcList", pcList);
            result.data("roleInfoAppList", appList);
            return result;
        } else {
            return Result.error().code(R_Code.R_NULL_DATA_FAIL);
        }
    }

    @Override
    public Result getAllPermissionToAdd(Result result, Integer userId, Integer deanId) {
        if (userId != null && deanId != null && deanId == userId) {
            if (result == null) {
                result = Result.ok();
            }
            List<CustomRoleManageViewAndUpdatePullDownResponseBean> allInfoPc = baseMapper.getAllInfoFromDean(userId, deanId, "0");
            List<CustomRoleManageViewAndUpdatePullDownResponseBean> allInfoApp = baseMapper.getAllInfoFromDean(userId, deanId, "1");
            result.data("allInfoPc", allInfoPc);
            result.data("allInfoApp", allInfoApp);
            return result;
        } else {
            return Result.error().code(R_Code.R_NULL_DATA_FAIL);
        }
    }
}
